package service

import (
	"crow-samba/app/pkg"
	"errors"
	"fmt"
	"github.com/toolkits/pkg/logger"
	"strings"
)

type Officer struct {
	Config        SambaConfig
	DirectoryList []Directory
}

// DirList2Config
// 将directory翻译成config文件
func (o *Officer) DirList2Config() (config SambaConfig) {
	o.Config = SambaConfig{}
	for _, d := range o.DirectoryList {
		//实例化一个part
		p := CreateConfigPart(d.DirTag, d.Path)
		//添加写用户
		writerS := strings.Join(d.WriterList, ",")
		p.AddEntry("write list", writerS)
		//将part加入config
		o.Config.AddPart(*p)
	}
	return o.Config
}

// GetDirectoryList
// 从配置文件获取DirectoryList
func (o *Officer) GetDirectoryList() (err error) {
	err = o.Config.GetConf()
	if err != nil {
		return err
	}
	for _, part := range o.Config.PartList {
		//配置文件中跳过global设置
		if part.Name == "[global]" {
			continue
		}
		//将part的信息转换为设置directory的信息
		tag1 := strings.TrimSuffix(part.Name, "]")
		tag2 := strings.Trim(tag1, "[")
		writerList := strings.Split(part.EntryMap["write list"], ",")
		//设置director
		directory := &Directory{}
		directory.Set(tag2, part.EntryMap["path"], writerList)
		fmt.Println(directory)
		//将该part转换的directory添加到结果
		o.DirectoryList = append(o.DirectoryList, *directory)
	}
	return err
}

// GetDirectory
// 从配置文件中获取指定Directory
func (o *Officer) GetDirectory(dirTag string) (directory Directory, err error) {
	err = o.Config.GetConf()
	if err != nil {
		return directory, err
	}
	for _, part := range o.Config.PartList {
		//global不是目录，跳过
		if part.Name == "[global]" {
			continue
		}
		if part.Name == pkg.DirTag2PathName(dirTag) {
			writerList := strings.Split(part.EntryMap["write list"], ",")
			//设置director
			directory.Set(part.Name, part.EntryMap["path"], writerList)
			return directory, err
		}
	}
	return directory, errors.New("未查询到结果")
}

// AddDirectories
// 配置文件中加入
func (o *Officer) AddDirectories() (err error) {
	err = o.Config.GetConf()
	if err != nil {
		return err
	}
	for _, d := range o.DirectoryList {
		//在服务器上传进目录
		err = d.MakeDir()
		if err != nil {
			logger.Error(err)
		}
		//为配置文件添加part
		p := CreateConfigPart(d.DirTag, d.Path)
		//p.AddEntry()
		//将part加入配置文件
		o.Config.AddPart(*p)

	}
	//写入配置文件
	_, err = o.Config.WriteConf()
	if err != nil {
		return err
	}
	return nil
}

// RemoveWriters
// 从配置文件中移除指定用户
func (o *Officer) RemoveWriters(userNames string) (err error) {
	delUserList := strings.Split(userNames, ",")
	d := o.DirectoryList[0]
	//获取配置文件
	err = o.Config.GetConf()
	if err != nil {
		return err
	}
	//从配置文件中删除配置文件中对应part的writer
	partName := pkg.DirTag2PathName(d.DirTag)
	o.Config.RemoveWriters(partName, delUserList)

	//将新配置写入配置文件
	_, err = o.Config.WriteConf()
	if err != nil {
		return err
	}
	return nil
}

// AddWriter
// 为配置文件添加写用户
func (o *Officer) AddWriter(addUserList []string) (newWriterList []string, err error) {
	//只为一个项目添加
	d := o.DirectoryList[0]
	err = o.Config.GetConf()
	if err != nil {
		return nil, err
	}
	//遍历所有目录
	o.Config.AddWriter(d.DirTag, addUserList)

	//将新配置入配置文件
	_, err = o.Config.WriteConf()
	if err != nil {
		return newWriterList, err
	}
	return newWriterList, nil
}

func (o *Officer) DeletePart(dirTag string) (err error) {
	err = o.Config.GetConf()
	partName := pkg.DirTag2PathName(dirTag)
	newConfig := o.Config.DeletePart(partName)
	_, err = newConfig.WriteConf()
	if err != nil {
		return err
	}
	return nil
}

func (o *Officer) SetConfigPartWriters() (err error) {
	//输入判断
	if len(o.DirectoryList) == 0 {
		return errors.New("目录为空")
	}
	if len(o.DirectoryList) > 1 {
		return errors.New("只能修改一个目录")
	}
	writers := strings.Join(o.DirectoryList[0].WriterList, ",")
	//获取配置文件
	err = o.Config.GetConf()
	if err != nil {
		return err
	}
	//var part ConfigPart
	for i, p := range o.Config.PartList {
		partName := pkg.DirTag2PathName(o.DirectoryList[0].DirTag)
		if p.Name == partName {
			p.AddEntry("write list", writers)
			o.Config.PartList[i] = p
			break
		}
	}
	//写入配置文件
	_, err = o.Config.WriteConf()
	if err != nil {
		return err
	}
	return nil
}
